package cn.lccabc.dichotomy.no0050;

/**
 * @program: leet-code
 * @description:
 * 实现 pow(x, n) ，即计算 x 的 n 次幂函数。
 *
 * 示例 1:
 *
 * 输入: 2.00000, 10
 * 输出: 1024.00000
 * 示例 2:
 *
 * 输入: 2.10000, 3
 * 输出: 9.26100
 * 示例 3:
 *
 * 输入: 2.00000, -2
 * 输出: 0.25000
 * 解释: 2-2 = 1/22 = 1/4 = 0.25
 * 说明:
 *
 * -100.0 < x < 100.0
 * n 是 32 位有符号整数，其数值范围是 [−231, 231 − 1] 。
 *
 * @author: LiCC
 * @create: 2020-02-25 20:28
 */
public class Solution {

    public static double myPow(double x, long n) {
        if (n < 0){
            x = 1 / x;
            n = -n;
        }
        return fastPow(x, n);
    }

    private static double fastPow(double x, long n){
        if (n == 0){
            return 1.0;
        }
        double half = fastPow(x, n / 2);
        if (n % 2 == 0){
            return half * half;
        } else {
            return half * half * x;
        }
    }

    public static void main(String[] args) {
        double x = 2.00000;
        int n = -10;
        double result = myPow(x, n);
        System.out.println(result);
    }
}
